package com.RLFZ.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.RLFZ.entity.BanJi;
import com.RLFZ.entity.User;
import com.RLFZ.entity.XueYuan;
import com.RLFZ.service.BanJiService;
import com.RLFZ.service.RoleService;
import com.RLFZ.service.UserService;
import com.RLFZ.service.XueYuanService;
import com.RLFZ.vo.DataView;
import com.RLFZ.vo.UserVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 服务控制器
 *
 * @author zh
 * @since 2022-08-25 17:16:58
 * @description 
 */
@Slf4j
@RequiredArgsConstructor
@Controller
@RequestMapping("/user")
public class UserController  {
    @Autowired
    private UserService userService;

    @Autowired
    private BanJiService banJiService;

    @Autowired
    private XueYuanService xueYuanService;

    @Autowired
    private RoleService roleService;


    @RequestMapping("toUser")
    public String toUser(){
        return "user/user";
    }

    @RequestMapping("toChangePassword")
    public String toChangePassword(){
        return "user/changepassword";
    }

    @RequestMapping("toUserInfo")
    public String toUserInfo(){
        return "user/userInfo";
    }

    /**
     * 带有分页的查询所有数据展示
     * @param userVo
     * @return
     */
    @RequestMapping("/loadAllUser")
    @ResponseBody
    public DataView getAllUser(UserVo userVo){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        IPage<User> page=new Page<>(userVo.getPage(),userVo.getLimit());
        queryWrapper.like(StringUtils.isNoneBlank(userVo.getUsername()),"username",userVo.getUsername());
        queryWrapper.eq(StringUtils.isNoneBlank(userVo.getPhone()),"phone",userVo.getPhone());
        IPage<User> iPage = userService.page(page, queryWrapper);
        for (User user : iPage.getRecords()){
            // 为班级名字进行赋值
            if (user.getBanJiId()!=null){
                // 班级banJiService查库
                BanJi banji = banJiService.getById(user.getBanJiId());
                user.setBanJiName(banji.getName());
            }
            // 为学院名字进行赋值
            if (user.getXueYuanId()!=null){
                XueYuan xueYuan = xueYuanService.getById(user.getXueYuanId());
                user.setXueYuanName(xueYuan.getName());
            }
            // 为老师名字进行赋值
            if (user.getTeacherId()!=null){
                User teacher = userService.getById(user.getTeacherId());
                user.setTeacherName(teacher.getUsername());
            }
        }
        return new DataView(iPage.getTotal(),iPage.getRecords());
    }

    /**
     * 添加角色
     * @param user
     * @return
     */
    @RequestMapping("/addUser")
    @ResponseBody
    public DataView adduser(User user){
        DataView dataView=new DataView();
        user.setImg("/upload/5c2764f9-5aa8-4d8c-a775-dde0e1f6eeae.jpeg");
        userService.save(user);
        dataView.setMsg("角色插入成功");
        dataView.setCode(200);
        return dataView;
    }
    /**
     * 初始化下拉菜单的数据[BanJi]
     */
    @RequestMapping("/listAllBanJi")
    @ResponseBody
    public List<BanJi> listAllBanJi(){
        List<BanJi> list=banJiService.list();
        return list;
    }
    /**
     * 新增用户时
     *初始化下拉列表【班级】
     *
     */
    @RequestMapping("/listBanJi/{xueYuanId}")
    @ResponseBody
    public List<BanJi> listAllBanJi(@PathVariable("xueYuanId") Integer xueYuanId){
        QueryWrapper<BanJi>queryWrapper = new QueryWrapper<>();
        if(xueYuanId != null){
            queryWrapper.eq("xue_yuan_id",xueYuanId);
        }
        return banJiService.list(queryWrapper);
    }
    /**
     * 初始化下拉菜单的数据[XueYuan]
     */
    @RequestMapping("/listAllXueYuan")
    @ResponseBody
    public List<XueYuan> listAllXueYuan(){
        List<XueYuan> list=xueYuanService.list();
        return list;
    }
    /**
     * 修改角色
     */
    @RequestMapping("/updateUser")
    @ResponseBody
    public DataView updateuser(User user){
        userService.updateById(user);
        DataView dataView=new DataView();
        dataView.setCode(200);
        dataView.setMsg("更新角色成功！");
        return dataView;
    }
    /**
     * 删除角色
     */
    @RequestMapping("/deleteRole/{id}")
    @ResponseBody
    public DataView deleteMenu(@PathVariable("id") Integer id){
        userService.removeById(id);
        DataView dataView=new DataView();
        dataView.setCode(200);
        dataView.setMsg("删角色成功！");
        return dataView;
    }
    /**
     * 重置密码
     */
    @RequestMapping("/resetPwd/{id}")
    @ResponseBody
    public DataView resetPwd(@PathVariable("id") Integer id){
        User user=new User();
        user.setId(id);
        user.setPassword("123456");
        userService.updateById(user);
        DataView dataView=new DataView();
        dataView.setCode(200);
        dataView.setMsg("用户重置密码成功！");
        return dataView;
    }
    /**
     * 点击分配初始化用户角色
     */
    @RequestMapping("/initRoleByUserId")
    @ResponseBody
    public DataView initRoleByUserId(Integer id){
        // 查询角色all
        List<Map<String, Object>> listMaps = roleService.listMaps();
        // 查询当前用户所拥有的角色
        List<Integer> currentUserRoles=roleService.queryUserRoleById(id);
        //3.让你的前端变为选中状态
        for (Map<String,Object> map : listMaps){
            Boolean LAY_CHECKED=false;
            Integer roleId=(Integer) map.get("id");
            for (Integer rid: currentUserRoles){
                if (rid.equals(roleId)){
                    LAY_CHECKED=true;
                    break;
                }
            }
            map.put("LAY_CHECKED",LAY_CHECKED);
        }
        return new DataView(Long.valueOf(listMaps.size()),listMaps);

    }
    /**
     * 保存用户角色之间的关系 1:m
     * 先删除后保存
     */
    @RequestMapping("/saveUserRole")
    @ResponseBody
    public DataView saveUserRole(Integer uid, Integer[] ids){
        userService.saveUserRole(uid,ids);
        DataView dataView=new DataView();
        dataView.setCode(200);
        dataView.setMsg("角色分配成功");
        return dataView;
    }
}